﻿using System.Drawing;
using SpImageCasheSDK;

namespace SpImageCacheActions
{
    class SpImageCacheGrayScaleAction : ISpImageCacheAction
    {
        public static Bitmap MakeGrayscale(Bitmap original)
        {
            for (int i = 0; i < original.Width; i++)
            {
                for (int j = 0; j < original.Height; j++)
                {
                    Color originalColor = original.GetPixel(i, j);

                    int grayScale = (int)((originalColor.R * .3) + (originalColor.G * .59) + (originalColor.B * .11));

                    Color newColor = Color.FromArgb(grayScale, grayScale, grayScale);

                    original.SetPixel(i, j, newColor);
                }
            }

            return original;
        }

        public Image Execute(Image image, params object[] actionParameters)
        {
            return MakeGrayscale((Bitmap)image);
        }
    }
}
